//
//  CalcView.m
//  JiaoX
//
//  Created by yinqy on 15/1/14.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//  商品数量加减的view

#import "CalcView.h"

#define CALCVIEW_HEIGHT 34
#define CALCVIEW_WIDTH 129
#define BTN_FONT_SIZE 26

@interface CalcView()
{
    UIButton *_leftBtn;
    
    
    UIButton *_RightBtn;
    
    NSUInteger _prodSum;
}

@end

@implementation CalcView

- (instancetype)initWithWidth:(CGFloat)width Height:(CGFloat)height addImage:(NSString *)addImage minusImage:(NSString *)minusImage;
{
    self = [super init];
    if (self) {
        _prodSum = 1;
        JXLog(@"width=%d;height=%d",(int)width,(int)height);
        [self setBounds:CGRectMake(0, 0, (int)width, (int)height)];
        _numLabbel = [[UIButton alloc]initWithFrame:CGRectMake(width/3, 0, width/3, height)];
        [_numLabbel setTitle:@"1" forState:UIControlStateNormal];
        [_numLabbel setBackgroundImage:[UIImage resizedImage:@"bg_calc_center_cart"] forState:UIControlStateNormal];
        [_numLabbel setUserInteractionEnabled:NO];
        [_numLabbel setTitleColor:JXColor(128, 128, 128) forState:UIControlStateNormal];
        
        [self addSubview:_numLabbel];
        
        _leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width/3, height)];
        [_leftBtn.titleLabel setFont:[UIFont systemFontOfSize:BTN_FONT_SIZE]];
        [_leftBtn setTitleColor:JXColor(128, 128, 128) forState:UIControlStateNormal];
        
        [_leftBtn setBackgroundImage:[UIImage resizedImage:@"bg_calc_left_test"] forState:UIControlStateNormal];
        [_leftBtn setBackgroundImage:[UIImage resizedImage:@"bg_calc_left_test_highlight"] forState:UIControlStateHighlighted];
        
        [_leftBtn setImage:[UIImage imageNamed:minusImage] forState:UIControlStateNormal];
        [_leftBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlight",minusImage]] forState:UIControlStateHighlighted];
        
        [_leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftBtn];
        
        
        _RightBtn = [[UIButton alloc]initWithFrame:CGRectMake(width/3*2, 0, width/3, height)];
        [_RightBtn.titleLabel setFont:[UIFont systemFontOfSize:BTN_FONT_SIZE]];
        [_RightBtn setTitleColor:JXColor(128, 128, 128) forState:UIControlStateNormal];
        
        [_RightBtn setBackgroundImage:[UIImage resizedImage:@"bg_calc_right_test"] forState:UIControlStateNormal];
        [_RightBtn setBackgroundImage:[UIImage resizedImage:@"bg_calc_right_test_highlight"] forState:UIControlStateHighlighted];
        
        [_RightBtn setImage:[UIImage imageNamed:addImage] forState:UIControlStateNormal];
        [_RightBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlight",addImage]] forState:UIControlStateHighlighted];
        [self setBackgroundColor:[UIColor whiteColor]];
        [_RightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_RightBtn];
        
//        [self.layer setBorderWidth:0.7];
//        [self.layer setCornerRadius:5];
//        [self.layer setMasksToBounds:YES];
    }
    return self;
}

-(void)leftBtnClick:(UIButton *)btn{
    
    --_prodSum;
    [self numChage:_prodSum+1 newNum:_prodSum position:self.tag calcView:self];
    if(_prodSum<1){
        ++_prodSum;
        return;
    }
    [_numLabbel setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_prodSum] forState:UIControlStateNormal];
    
}

-(void)rightBtnClick:(UIButton *)btn{
    ++_prodSum;
    [self numChage:_prodSum-1 newNum:_prodSum position:self.tag calcView:self];
    if(_prodSum>20){
        --_prodSum;
        return;
    }
    [_numLabbel setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)_prodSum] forState:UIControlStateNormal];
}

-(void)setNum:(NSUInteger)num{
    _prodSum = num;
    [_numLabbel setTitle:[NSString stringWithFormat:@"%lu",(unsigned long)num] forState:UIControlStateNormal];
}

-(void)numChage:(NSUInteger)oldNum newNum:(NSUInteger)newNum position:(NSUInteger)position calcView:(CalcView *)calcView{
    if ([_delegate respondsToSelector:@selector(numChange:newNum:position:calcView:)]) {
        [_delegate numChange:oldNum newNum:newNum position:position calcView:calcView];
    }
}

-(NSUInteger)getNum{
    return [_numLabbel.titleLabel.text integerValue];
}

@end
